<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="100px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">

          <el-col :lg="8" :md="12">
            <el-form-item label="选择时间:">
              <el-date-picker
                style="width: 100%"
                v-model="where.time_range"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                label-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="选择游戏">
              <PageSelect
                :placeholder="'请选择游戏'"
                v-model="where.game_id"
                :whereName="'name'"
                :url="'/BoxRebate/getGameList'"
                :optionLable="'game_name'"
                :optionvalue="'id'"
                :multiple="false"
              />
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="选择区服:">
              <PageSelect
                :placeholder="'请选择区服'"
                v-model="where.open_id"
                :whereName="'name'"
                :url="openUrl"
                :optionLable="'name'"
                :optionvalue="'id'"

              />
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
              >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:box-privilege-title:add')"
          >添加
          </el-button>
        </template>

        <template slot="avatar" slot-scope="{ row }">
          <el-image
            style="width: 30px; height: 30px"
            :src="row.icon"
            fit="fit"
          ></el-image>
        </template>
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:box-privilege-title:edit')"
          >编辑
          </el-link>

          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="status(row)"
          >{{row.status === 0? "上架" : "下架"}}
          </el-link>

        </template>


        <!-- 状态 -->
        <template slot="status" slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="primary" size="small">开启</el-tag>
          <el-tag v-if="row.status === 0" type="success" size="small">关闭</el-tag>
        </template>

      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <Edit :data="current" :visible.sync="showEdit" @done="reload" v-if="showEdit"/>
  </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import Edit from "./edit";
    import PageSelect from "@/components/pageSelect/index.vue";

    export default {
        name: "Rebate",
        components: {Edit, PageSelect},
        computed: {
            ...mapGetters(["permission"])
        },
        data() {
            return {
                // 表格数据接口
                url: "/BoxRebate/index",
                // 表格列配置
                columns: [
                    {
                        prop: "id",
                        label: "序号",
                        width: 100,
                        align: "center",
                        showOverflowTooltip: true,
                        fixed: "left"
                    },
                    {
                        prop: "game_name",
                        label: "游戏名称",
                        showOverflowTooltip: true,
                        minWidth: 200,
                        align: "center"
                    },
                    {
                        prop: "open_name",
                        label: "区服名称",
                        showOverflowTooltip: true,
                        minWidth: 200,
                        align: "center"
                    },
                    {
                        prop: "send_type_name",
                        label: "发放方式",
                        showOverflowTooltip: true,
                        minWidth: 200,
                        align: "center"
                    },
                    {
                        prop: "gradient_data",
                        label: "返利梯度",
                        showOverflowTooltip: true,
                        align: "center",
                        minWidth: 200
                    },
                    {
                        prop: "rule",
                        label: "返利规则",
                        showOverflowTooltip: true,
                        align: "center",
                        minWidth: 200
                    },
                    {
                        prop: "status",
                        label: "开启状态",
                        showOverflowTooltip: true,
                        minWidth: 200,
                        align: "center",
                        slot: "status"
                    },
                    {
                        prop: "remark",
                        label: "备注",
                        showOverflowTooltip: true,
                        align: "center",
                        minWidth: 200
                    },
                    {
                        prop: "create_time",
                        label: "配置时间",
                        sortable: "custom",
                        showOverflowTooltip: true,
                        minWidth: 160,
                        align: "center",
                        formatter: (row, column, cellValue) => {
                            return this.$util.toDateString(cellValue);
                        }
                    },
                    {
                        columnKey: "action",
                        label: "操作",
                        width: 150,
                        align: "center",
                        resizable: false,
                        slot: "action",
                        fixed: "right"
                    }
                ],
                // 表格搜索条件
                where: {},
                // 表格选中数据
                selection: [],
                // 当前编辑数据
                current: null,
                // 当前行id数据
                gameRowID: null,
                // 是否显示编辑弹窗
                showEdit: false,
                openUrl: '/BoxRebate/getGameOpenList',
                tagStatus: {}
            };
        },
        watch: {
            "where.game_id"(val) {
                console.log(val)
                this.openUrl = `/BoxRebate/getGameOpenList?game_id=` + val;
            }
        },
        methods: {
            /* 刷新表格 */
            reload() {
                this.$refs.table.reload({where: this.where});
            },
            /* 重置搜索 */
            reset() {
                this.where = {};
                this.reload();
            },
            /* 显示编辑 */
            openEdit(row) {
                this.current = row;
                this.showEdit = true;
            },

            status(row) {
                if (row.status === 1) {
                    this.tagStatus = {status: 0, id: row.id};
                } else {
                    this.tagStatus = {status: 1, id: row.id};
                }
                this.loading = true
                this.$http
                    .post('/BoxRebate/status', this.tagStatus)
                    .then((res) => {
                        this.loading = false
                        if (res.data.code === 0) {
                            this.$message.success(res.data.msg)
                            this.$emit('done')
                            this.reload()
                        } else {
                            this.$message.error(res.data.msg)
                        }
                    })
                    .catch((e) => {
                        this.loading = false
                        this.$message.error(e.message)
                    })
            }
        }
    };
</script>

<style scoped></style>
